package com.neusoft.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.client.ServiceInstance;

import com.neusoft.feign.FoodFeignClient;
import com.neusoft.po.Business;
import com.neusoft.po.CommonResult;
import com.neusoft.po.Food;
import com.neusoft.service.BusinessService;

@RefreshScope //开启动态刷新
//@CrossOrigin("*")
@RestController
@RequestMapping("/Business")
public class BusinessController {

	@Autowired
	private BusinessService businessService;
	@Autowired
	private FoodFeignClient foodFeignClient;

	@GetMapping("/OrderTypeId")
	public CommonResult<List> listBusinessByOrderTypeId(Business business)
			throws Exception {
		List<Business> list = businessService.listBusinessByOrderTypeId(business.getOrderTypeId());
		return new CommonResult(200, "success", list);
	}

	@GetMapping("/BusinessId")
	public CommonResult<Business> getBusinessById(Business business) throws Exception {
		Business bus = businessService.getBusinessById(business.getBusinessId());
		// 在商家微服务中调用食品微服务
		CommonResult<List> result = foodFeignClient.listFoodByBusinessId(business.getBusinessId());
		// 如果食品微服务返回降级响应，那么就返回空集合
		if (result.getCode() == 200) {
			business.setFoodList(result.getResult());
		} else {
			business.setFoodList(new ArrayList());
		}
		return new CommonResult(200, "success", bus);
	}

}